package com.etrans.web.gps.business.enterpriseTask.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.business.enterpriseTask.domian.EnterpriseSwap;
import com.etrans.web.gps.business.enterpriseTask.domian.EnterpriseVo;
import com.etrans.web.gps.business.enterpriseTask.service.SendEntMessageService;
import com.etrans.web.gps.util.DateUtil;

@Controller
@RequestMapping("/mail/*")
public class enterpriseEmailController {

	private static final Logger logger = LoggerFactory.getLogger(enterpriseEmailController.class);

	@Autowired
	private SendEntMessageService sendMessageService;
	
	@ResponseBody
	@RequestMapping(value="enterprise/sendMailByWeek.html",produces="text/html;charset=UTF-8")
	public void sendMailByWeek(HttpServletRequest request,HttpServletResponse response ,String previousMonth,String startStr ,String endStr ,String id){
		try {
			response.setContentType("text/plain");  
		    response.setHeader("Pragma", "No-cache");  
		    response.setHeader("Cache-Control", "no-cache");  
		    response.setDateHeader("Expires", 0);  
		    response.setCharacterEncoding("utf-8");
			Map<String,Object> param = new HashMap<>();
			param.put("id", id);
			List<EnterpriseVo> entList = sendMessageService.queryAllEnterprise(param);
			List<EnterpriseSwap> entSwapList = sendMessageService.queryEnterpriseSwapByWeek(startStr);
			String Time = "week";
			String msgTime = "(" + startStr + "至" + endStr + ")业户周考核统计";
			boolean flag = false;
			List<Map<String,Object>> result = sendMessageService.sendMail(entList,entSwapList,msgTime,Time,flag);
			Map<String,Object> temp = result.get(0);
			temp.put("time", "第"+DateUtil.getDayOfWeek(DateUtil.getStrToDate(startStr+" 00:00:00"))+"周"+msgTime);
			PrintWriter out = response.getWriter();       
			Object resultJSON = JSONObject.toJSON(result.get(0)); //根据需要拼装json  
			String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数  
			out.println(jsonpCallback+"("+resultJSON+")");//返回jsonp格式数据  
			out.flush();  
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
	}
	
	@ResponseBody
	@RequestMapping("enterprise/sendMailByMonth.html")
	public void sendMailByMonth(HttpServletRequest request,HttpServletResponse response ,String previousMonth,String startStr ,String endStr ,String id){
		try {
			response.setContentType("text/plain");  
		    response.setHeader("Pragma", "No-cache");  
		    response.setHeader("Cache-Control", "no-cache");  
		    response.setDateHeader("Expires", 0);  
		    response.setCharacterEncoding("utf-8");
			Map<String,Object> param = new HashMap<>();
			param.put("id", id);
			List<EnterpriseVo> entList = sendMessageService.queryAllEnterprise(param);
			List<EnterpriseSwap> entSwapList = sendMessageService.queryEnterpriseSwapByMonth(previousMonth);
			String Time = "month";
			boolean flag = false;
			String msgTime = "("+previousMonth+"月)业户月考核统计";
			List<Map<String,Object>> result = sendMessageService.sendMail(entList,entSwapList,msgTime,Time,flag);
			Map<String,Object> temp =result.get(0);
			temp.put("time", msgTime);
			PrintWriter out = response.getWriter();       
			Object resultJSON = JSONObject.toJSON(result.get(0)); //根据需要拼装json  
			String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数  
			out.println(jsonpCallback+"("+resultJSON+")");//返回jsonp格式数据  
			out.flush();  
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
